package v1

import (
	"auth/bll"
	"auth/model"
	"auth/server/web/middleware"
	"auth/utils"
	"github.com/gin-gonic/gin"
)

var CasbinService = &casbinService{}

type casbinService struct{}

func (a *casbinService) Init(r *gin.RouterGroup) {
	g := r.Group("/casbin", middleware.Auth())
	{
		g.POST("/getPolicyPathByAuthorityId", a.getPolicyPathByAuthorityId)
		g.POST("/updateCasbin", a.updateCasbin)
	}
}

func (a *casbinService) updateCasbin(c *gin.Context) {
	var (
		in  = &model.CasbinUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.CasbinService.UpdateCasbin(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

func (a *casbinService) getPolicyPathByAuthorityId(c *gin.Context) {
	var (
		in  = &model.PolicyPathRequest{}
		out = &model.PolicyPathResponse{}
		err error
	)
	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}
	if out, err = bll.CasbinService.GetPolicyPathByAuthorityId(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, out)
}
